package com.example.hrsystem.data.source;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.example.hrsystem.data.model.AttendanceRecord;
import com.example.hrsystem.data.model.TaskItem;
import com.example.hrsystem.data.model.User;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 数据库初始化器
 * 用于在应用首次启动时添加模拟数据
 */
public class DatabaseInitializer {

    private static final String TAG = "DatabaseInitializer";

    /**
     * 初始化数据库，添加模拟数据
     */
    public static void initializeDatabase(final Context context) {
        new InitializeDbAsyncTask(context).execute();
    }

    private static class InitializeDbAsyncTask extends AsyncTask<Void, Void, Void> {
        private Context context;

        private InitializeDbAsyncTask(Context context) {
            this.context = context;
        }

        @Override
        protected Void doInBackground(Void... voids) {
            try {
                Log.d(TAG, "开始数据库初始化");
                AppDatabase database = AppDatabase.getDatabase(context.getApplicationContext());
                Log.d(TAG, "成功获取数据库实例");
                
                // 检查是否已有用户数据，如果没有则添加模拟数据
                Log.d(TAG, "检查用户数据是否存在");
                
                // 修复LiveData获取数据的问题
                List<User> users = database.userDao().getAllUsersDirect();
                int userCount = users != null ? users.size() : 0;
                Log.d(TAG, "当前用户数量: " + userCount);
                
                if (userCount == 0) {
                    Log.d(TAG, "用户数量为0，开始添加模拟数据");
                    Log.d(TAG, "添加模拟用户数据");
                    // 添加模拟用户
                    User user = new User();
                    user.setName("测试用户");
                    user.setPhoneNumber("13800138000");
                    user.setPassword("123456");
                    user.setEmployeeId("EMP001");
                    user.setDepartment("技术部");
                    user.setPosition("开发工程师");
                    user.setEmail("test@example.com");
                    user.setEntryDate("2023-01-01");
                    user.setUserType("普通员工");
                    user.setActive(true);
                    
                    database.userDao().insert(user);
                    
                    // 添加一些模拟打卡记录
                    AttendanceRecord record1 = new AttendanceRecord();
                    record1.setEmployeeId(user.getUserId() + "");
                    record1.setCheckInTime(new Date(System.currentTimeMillis() - 3600000 * 8)); // 8小时前
                    record1.setCheckOutTime(new Date(System.currentTimeMillis() - 3600000)); // 1小时前
                    record1.setCheckInLocation("公司办公室");
                    record1.setCheckOutLocation("公司办公室");
                    record1.setAttendanceType("正常");
                    record1.setCompleted(true);
                    
                    AttendanceRecord record2 = new AttendanceRecord();
                    record2.setEmployeeId(user.getUserId() + "");
                    record2.setCheckInTime(new Date(System.currentTimeMillis() - 3600000 * 24 * 1)); // 昨天
                    record2.setCheckOutTime(new Date(System.currentTimeMillis() - 3600000 * 16 * 1)); // 昨天
                    record2.setCheckInLocation("公司办公室");
                    record2.setCheckOutLocation("公司办公室");
                    record2.setAttendanceType("正常");
                    record2.setCompleted(true);
                    
                    // 添加今天的打卡记录
                    AttendanceRecord record3 = new AttendanceRecord();
                    record3.setEmployeeId(user.getUserId() + "");
                    record3.setCheckInTime(new Date(System.currentTimeMillis() - 3600000 * 4)); // 4小时前
                    record3.setCheckInLocation("公司办公室");
                    record3.setAttendanceType("正常");
                    record3.setCompleted(false);
                    
                    database.attendanceDao().insert(record1);
                    database.attendanceDao().insert(record2);
                    database.attendanceDao().insert(record3);
                    
                    // 添加一些模拟任务
                    Log.d(TAG, "开始添加模拟任务数据");
                    TaskItem task1 = new TaskItem();
                    task1.setCreatorId(user.getUserId() + "");
                    task1.setTitle("完成项目需求文档");
                    task1.setContent("详细描述项目的功能需求和技术实现方案");
                    task1.setTaskType("文档");
                    task1.setStatus("进行中");
                    task1.setPriority("高");
                    task1.setCreateTime(new Date(System.currentTimeMillis() - 3600000 * 24 * 3)); // 3天前
                    task1.setDeadline(new Date(System.currentTimeMillis() + 3600000 * 24 * 2)); // 2天后
                    task1.setAssigneeId(user.getUserId() + "");
                    task1.setRemark("请按时完成");
                    
                    TaskItem task2 = new TaskItem();
                    task2.setCreatorId(user.getUserId() + "");
                    task2.setTitle("参加团队周会");
                    task2.setContent("讨论上周工作进展和本周计划");
                    task2.setTaskType("会议");
                    task2.setStatus("待办");
                    task2.setPriority("中");
                    task2.setCreateTime(new Date(System.currentTimeMillis() - 3600000 * 12)); // 12小时前
                    task2.setDeadline(new Date(System.currentTimeMillis() + 3600000 * 12)); // 12小时后
                    task2.setAssigneeId(user.getUserId() + "");
                    
                    database.taskDao().insert(task1);
                    database.taskDao().insert(task2);
                    
                    Log.d(TAG, "模拟数据添加完成！用户ID: " + user.getUserId() + ", 添加了2条打卡记录和2条任务");
                } else {
                    Log.d(TAG, "数据库中已有用户数据，跳过初始化");
                }
                
            } catch (Exception e) {
                Log.e(TAG, "初始化数据库失败: " + e.getMessage(), e);
            }
            
            Log.d(TAG, "数据库初始化过程结束");
            return null;
        }
    }
}